package com.cqc.pullloadmorerecyclerviewdemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;

import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private PullLoadMoreRecyclerView refreshRecyclerView;
    private RecyclerView recyclerView;
    private List<String> list = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();

        refreshRecyclerView = (PullLoadMoreRecyclerView) findViewById(R.id.pullLoadMoreRecyclerView);
        refreshRecyclerView.setLinearLayout();
//        refreshRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        refreshRecyclerView.addItemDecoration(new DividerItemDecoration(MainActivity.this, DividerItemDecoration.VERTICAL));
        adapter = new MyAdapter(list);
        refreshRecyclerView.setAdapter(adapter);


//        refreshRecyclerView.setRefreshing(false);//刷新中
//        refreshRecyclerView.setPullRefreshEnable(false);//禁止下拉刷新
//        refreshRecyclerView.setPushRefreshEnable(false);//禁止上拉刷新

//        refreshRecyclerView.setColorSchemeResources(R.color.color1,R.color.color2,R.color.color3,R.color.color4);
        //设置脚布局的文字+文字颜色+背景色
//        refreshRecyclerView.setFooterViewBackgroundColor(R.color.colorPrimary);
//        refreshRecyclerView.setFooterViewText("上拉加载更多");
//        refreshRecyclerView.setFooterViewTextColor(R.color.colorAccent);

        refreshRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshRecyclerView.setPullLoadMoreCompleted();
                    }
                }, 1500);
            }

            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        list.add("add");
                        adapter.notifyItemInserted(list.size()-1);
                        refreshRecyclerView.setPullLoadMoreCompleted();
//                        refreshRecyclerView.scrollToTop();//滑动顶部
                    }
                }, 1500);
            }
        });
    }

    private void initData() {
        for (int i = 0; i < 50; i++) {
            list.add("item" + i);
        }
    }
}
